import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

/**
 * Displays the data from a StatsList or Stats
 *
 */
public class StatsPane extends JTabbedPane 
{
	private StatsList statsList;
	private DateList dateList;
	private JPanel statsListPanel;
	private JPanel dateListPanel;
	private JScrollPane statsListPane;
	private JScrollPane dateListPane;
	private User user;
	
	
	public StatsPane(User user)
	{
		this.user= user;		
		statsList= user.getStatsList();
		dateList= user.getDateList();
		statsListPanel= new JPanel();
		statsListPanel.setLayout(new BoxLayout(statsListPanel, BoxLayout.X_AXIS));
		dateListPanel= new JPanel();
		dateListPanel.setLayout(new BoxLayout(dateListPanel, BoxLayout.X_AXIS));
		makePanels();
		statsListPane= new JScrollPane(statsListPanel);
		dateListPane= new JScrollPane(dateListPanel);
		addTab("Stats", statsListPane);
		setMnemonicAt(0, KeyEvent.VK_S);
		if(Global.isApplet()==false)
		{
			addTab("Graph", dateListPane);
			setMnemonicAt(1, KeyEvent.VK_G);
		}
	}
	
	private void addIndividualPanels(JPanel panel, StatsList list)
	{
		panel.add(new IndividualPanel(list.getBaseStats()));
		for(StatsList.GameTypeNode k : list.getList())
		{
			IndividualPanel nextPanel= new IndividualPanel(k.getGame());
			nextPanel.head.setText("From: "+k.getMinValue()+" To "+k.getMaxValue());
			panel.add(nextPanel);
		}
		
	}
	
	private void makePanels()
	{
		addIndividualPanels(statsListPanel, statsList);
		/*for(DateList.DateNode k : dateList.getList())
		{
			JPanel nextPanel= new JPanel();
			nextPanel.setLayout(new BoxLayout(nextPanel, BoxLayout.Y_AXIS));
			nextPanel.add(new JLabel(dateFormat.format(k.getDate().getTime())));
			IndividualPanel baseStats= new IndividualPanel(k.getList().getBaseStats());
			baseStats.head.setText("");
			nextPanel.add(baseStats);
			addIndividualPanels(nextPanel, k.getList());
			dateListPanel.add(nextPanel);
		}*/
		if(dateList.getList().size()>0) // don't graph empty list
		{
			JTabbedPane datePane= new JTabbedPane();
			datePane.add("All", new GraphPane(dateList));
			for(StatsList.GameTypeNode k : statsList.getList())
			{
				datePane.add(k.getMinValue()+" To "+k.getMaxValue(),
				new GraphPane(dateList.getSubList(k.getMinValue(), k.getMaxValue())));
			}
			dateListPanel.add(datePane);
		}
	}
	
	private class IndividualPanel extends JPanel
	{
		private Stats stats;
		private DecimalFormat percentageFormat;
		private DecimalFormat timeFormat;
		private JLabel head;
		private JLabel correct;
		private JLabel percentage;
		private JLabel onFirst;
		private JLabel percentageOnFirst;
		private JLabel averageTime;
		private JPanel leftPanel;
		private JPanel rightPanel;
		
		/**
		 * Creates a new StatsPanel for a Stats
		 * 
		 * @param stats
		 */
		private IndividualPanel(Stats stats)
		{
			setLayout(new BorderLayout());
			leftPanel= new JPanel();
			leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
			rightPanel= new JPanel();
			rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
			add(leftPanel, BorderLayout.WEST);
			add(rightPanel, BorderLayout.EAST);
			percentageFormat= Global.PERCENTAGE_FORMAT;
			timeFormat= Global.TIME_FORMAT;
			this.stats= stats;
			assignLabels();
		}
		
		/**
		 * Creates the JLabels used to display the stats
		 */
		private void assignLabels()
		{
			leftPanel.add(new JLabel("     "));
			head= new JLabel(user.getName());
			leftPanel.add(head);
			correct= new JLabel("Correct: "+stats.getCorrect()+
						 " Incorrect: "+stats.getIncorrect());
			leftPanel.add(correct);
			double percentageCorrect= (double)stats.getCorrect()/
									  (double)(stats.getCorrect()+stats.getIncorrect());
			percentage= new JLabel("Precentage Correct: "+
									percentageFormat.format(percentageCorrect));
			leftPanel.add(percentage);
			onFirst= new JLabel("Correct On First Try: "+stats.getCorrectOnFirstTry()+
					 	 " Incorrect On First Try: "+stats.getIncorrectOnFirstTry()+" ");
			leftPanel.add(onFirst);
			double percentageCorrectOnFirst= (double)stats.getCorrectOnFirstTry()/
			  (double)(stats.getCorrectOnFirstTry()+stats.getIncorrectOnFirstTry());
			percentageOnFirst= new JLabel("Precentage Correct On First Try: "+
						 percentageFormat.format(percentageCorrectOnFirst));
			leftPanel.add(percentageOnFirst);
			double average= (double)stats.getAverageTime()/1000.0;
			averageTime= new JLabel("Average Time: "+timeFormat.format(average)+" ");
			leftPanel.add(averageTime);
			double min= (double)stats.getFastestTime()/1000.0;
			JLabel fastTime= new JLabel("Fastest Time: "+timeFormat.format(min)+" ");
			leftPanel.add(fastTime);
			
			for(int k=0; k<stats.getCorrectAtNumber().getSize(); k++)
			{
				JLabel atNumber= new JLabel("At "+(k+1)+": "+"Correct: "+
											stats.getCorrectAtNumber().get(k)+
											" Incorrect: "+
											stats.getIncorrectAtNumber().get(k)+
											" Average Time: "+
											timeFormat.format(
											stats.getAverageTimeAtNumber().get(k)/1000.0)
											+" ");
				leftPanel.add(atNumber);
			}
		}
	}

}
